﻿using HandDay.Framework.Common.Services;

namespace Handday.Erp.Transfer.Models
{
    /// <summary>
    /// erp基类
    /// </summary>
    public class ErpResultBase
    {
        /// <summary>
        /// 状态码：非0为异常
        /// </summary>
        public int StatusCode { get; set; }

        /// <summary>
        /// 异常消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 结果
        /// </summary>
        public bool Result { get; set; }

        /// <summary>
        /// 结果
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 成功结果
        /// </summary>
        public static ErpResultBase Succeed()
        {
            return new ErpResultBase { IsSuccess = true, Result = true, StatusCode = 0, Message = string.Empty };
        }

        /// <summary>
        /// 失败
        /// </summary>
        public static ErpResultBase Faild(int code, string message)
        {
            return new ErpResultBase { StatusCode = code, Message = message ?? string.Empty };
        }

        /// <summary>
        /// 结果
        /// </summary>
        public static ErpResultBase From(ServiceResult serviceResult)
        {
            var result = new ErpResultBase { StatusCode = serviceResult!.StatusCode, Message = serviceResult!.Message ?? string.Empty };
            result.Result = result.StatusCode == 0;
            result.IsSuccess = result.StatusCode == 0;
            return result;
        }
    }
}
